home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / OpenGL 1.0 SDK / Source / Libraries / glut / glut_init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  6.2 KB  |  323 lines  |  [TEXT/CWIE]

  1. /* Copyright (c) Mark J. Kilgard, 1994. */
  2.  
  3. /* This program is freely distributable without licensing fees
  4.    and is provided without guarantee or warrantee expressed or
  5.    implied. This program is -not- in the public domain. */
  6.  
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10.  
  11. #include "glut.h"
  12. #include "glutint.h"
  13.  
  14. #if defined(__MWERKS__) || defined(__SC__)
  15.     #include "console.h"
  16. #endif
  17.  
  18. #if defined(__MWERKS__)
  19.     #include "SIOUX.h"
  20. #endif
  21.  
  22. /* GLUT inter-file variables */
  23. char *__glutProgramName = NULL;
  24. int __glutArgc = 0;
  25. char **__glutArgv = NULL;
  26. char *__glutGeometry = NULL;
  27. GLboolean __glutDebug = GL_FALSE;
  28. static GLboolean __glutInited = GL_FALSE;
  29.  
  30. unsigned int __glutDisplayMode = GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH;
  31.   
  32. int __glutInitWidth = 300;
  33. int __glutInitHeight = 300;
  34. int __glutInitX = 50;
  35. int __glutInitY = 50;
  36.  
  37. static char *strdup(const char *string);
  38. static void parseGeometry(char *param, int *x, int *y, int *w, int *h);
  39.  
  40. static void DoInitManagers(void);
  41. static void DoInitConsole(void);
  42.  
  43. #if defined(__MWERKS__)
  44. extern OSErr __initialize(const CFragInitBlock *theInitBlock);
  45. extern void __terminate(void);
  46.  
  47. OSErr __glutInitLibrary(const CFragInitBlock *theInitBlock);
  48. void __glutTerminateLibrary(void);
  49.  
  50. OSErr __glutInitLibrary(const CFragInitBlock *theInitBlock)
  51. {
  52.     OSErr err;
  53.     
  54.     err = __initialize(theInitBlock);
  55.  
  56.     return err;
  57. }
  58.  
  59. void __glutTerminateLibrary(void)
  60. {
  61.     GLint i;
  62.     
  63.     /* Remove all windows */
  64.     for(i = 0; i < __glutWindowListSize; i++)
  65.     {
  66.         if(__glutWindowList[i])
  67.         {
  68.             __glutDestroyWindow(__glutWindowList[i], __glutWindowList[i]);
  69.         }
  70.     }
  71.     
  72.     if(__glutWindowList) free(__glutWindowList);
  73.     if(freeTimerList)    free(freeTimerList);
  74.     
  75.     /* Free command line args */
  76.     for(i = 0; i < __glutArgc; i++)
  77.     {
  78.         free(__glutArgv[i]);
  79.     }
  80.     
  81.     if(__glutArgc > 0) free(__glutArgv);
  82.     
  83.     /* Call default terminate */
  84.     __terminate();
  85. }
  86. #endif
  87.  
  88. static char *strdup(const char *string)
  89. {
  90.     char *new;
  91.  
  92.     new = malloc(strlen(string) + 1);
  93.     if (new == NULL)
  94.     return NULL;
  95.     strcpy(new, string);
  96.     return new;
  97. }
  98.  
  99. double __glutTime(void)
  100. {
  101.    UnsignedWide tk_time;
  102.    
  103.    Microseconds(&tk_time);
  104.    
  105.    return (4294.967296 * tk_time.hi + 0.000001 * tk_time.lo) * 1000.0;
  106. }
  107.  
  108. void __glutInitTime(double *beginning)
  109. {
  110.     static int beenhere = 0;
  111.     static double genesis;
  112.  
  113.     if (!beenhere) {
  114.         genesis = __glutTime();
  115.         beenhere = 1;
  116.     }
  117.     
  118.     *beginning = genesis;
  119. }
  120.  
  121. static void removeArgs(int *argcp, char **argv, int numToRemove)
  122. {
  123.     int i, j;
  124.  
  125.     for(i = 0, j = numToRemove; argv[j]; i++, j++) argv[i] = argv[j];
  126.  
  127.     argv[i] = NULL;
  128.     *argcp -= numToRemove;
  129. }
  130.  
  131. static void parseGeometry(char *param, int *x, int *y, int *w, int *h)
  132. {
  133.     int i, j;
  134.     char s[21], *sp;
  135.     char st[4] = {'x', '+', '+', '\0'};
  136.     int *pt[4];
  137.  
  138.     /* Default numbers */
  139.     *x = 20;
  140.     *y = 20;
  141.     *w = 300;
  142.     *h = 300;
  143.  
  144.     /* Init param pointers */
  145.     pt[0] = x;
  146.     pt[1] = y;
  147.     pt[2] = w;
  148.     pt[3] = h;
  149.  
  150.     sp = param;
  151.  
  152.     for(j = 0; j < 4; j++)
  153.     {
  154.         for(i = 0; (i < 20) && (*sp) && (*sp != st[j]); i++)
  155.         {
  156.             s[i] = *sp;
  157.             sp++;
  158.         }
  159.         
  160.         sp++;
  161.  
  162.         if(i == 20) return;
  163.  
  164.         s[i] = 0;
  165.  
  166.         *pt[j] = atoi(s);
  167.     }
  168. }
  169.  
  170. static void DoInitManagers(void)
  171. {
  172.     MaxApplZone();
  173.     MoreMasters();
  174.  
  175.     InitGraf(&qd.thePort);
  176.     InitFonts();
  177.     FlushEvents(everyEvent, 0);
  178.     InitWindows();
  179.     InitMenus();
  180.     TEInit();
  181.     InitDialogs(nil);
  182.     InitCursor();
  183. }
  184.  
  185. static void DoInitConsole(void)
  186. {
  187.     #if defined(__MWERKS__)
  188.         SIOUXSettings.initializeTB       = FALSE;
  189.         SIOUXSettings.standalone         = FALSE;
  190.         SIOUXSettings.setupmenus         = FALSE;
  191.         SIOUXSettings.autocloseonquit    = TRUE;
  192.         SIOUXSettings.asktosaveonclose   = TRUE;
  193.         SIOUXSettings.showstatusline     = TRUE;
  194.         SIOUXSettings.userwindowtitle    = NULL;
  195.         SIOUXSettings.tabspaces          = 4;
  196.         SIOUXSettings.columns            = 60;
  197.         SIOUXSettings.rows               = 8;
  198.         SIOUXSettings.toppixel           = 100;
  199.         SIOUXSettings.leftpixel          = 100;
  200.         SIOUXSettings.fontid             = kFontIDMonaco;
  201.         SIOUXSettings.fontsize           = 9;
  202.         SIOUXSettings.fontface           = normal;
  203.         SIOUXSettings.enabledraganddrop  = FALSE;
  204.         SIOUXSettings.outlinehilite      = FALSE;
  205.         SIOUXSettings.wasteusetempmemory = TRUE;
  206.     #endif
  207. }
  208.  
  209. void __glutInitApp(void)
  210. {
  211.     double unused;
  212.     
  213.     if(!__glutInited)
  214.     {
  215.         DoInitConsole();
  216.         DoInitManagers();
  217.         __glutBuildMenuBar();
  218.         __glutInitTime(&unused);
  219.         
  220.         __glutInited = GL_TRUE;
  221.     }
  222. }
  223.  
  224. void glutInitMac(int *argcp, char ***argv)
  225. {
  226.     double unused;
  227.     int i;
  228.  
  229.     #if defined(__MWERKS__) || defined(__SC__)
  230.         *argcp = ccommand(argv);
  231.     #endif
  232.  
  233.     /* Make private copy of command line arguments. */
  234.     __glutArgc = *argcp;
  235.     if(__glutArgc > 0)
  236.     {
  237.         __glutArgv = (char **) malloc(__glutArgc * sizeof(char *));
  238.         if(!__glutArgv) __glutFatalError("out of memory.");
  239.  
  240.         for(i = 0; i < __glutArgc; i++)
  241.         {
  242.             __glutArgv[i] = strdup((*argv)[i]);
  243.             if(!__glutArgv[i]) __glutFatalError("out of memory.");
  244.         }
  245.     }
  246.     else
  247.     {
  248.         __glutArgc = 1;
  249.         __glutArgv = (char **) malloc(__glutArgc * sizeof(char *));
  250.         __glutArgv[0] = strdup("GLUT Program");
  251.     }
  252.  
  253.     /* determine permanent program name */
  254.     __glutProgramName = __glutArgv[0];
  255.  
  256.     DoInitConsole();
  257.     DoInitManagers();
  258.     __glutBuildMenuBar();
  259.  
  260.     /* parse arguments for standard options */
  261.     for(i = 1; i < __glutArgc; i++)
  262.     {
  263.         if(!strcmp(__glutArgv[i], "-geometry"))
  264.         {
  265.             int x, y, width, height;
  266.  
  267.             if(++i >= __glutArgc)
  268.             {
  269.                 __glutFatalError("follow -geometry option with geometry parameter.");
  270.             }
  271.             
  272.             /* Fix bogus "{width|height} may be used before set" warning */
  273.             width  = 0;
  274.             height = 0;
  275.  
  276.             parseGeometry(__glutArgv[i], &x, &y, &width, &height);
  277.  
  278.             if(width > 0)  __glutInitWidth = width;
  279.             if(height > 0) __glutInitHeight = height;
  280.             
  281.             glutInitWindowSize(__glutInitWidth, __glutInitHeight);
  282.  
  283.             if(x >= 0) __glutInitX = x;
  284.             if(y >= 0) __glutInitY = y;
  285.  
  286.             glutInitWindowPosition(__glutInitX, __glutInitY);
  287.  
  288.             removeArgs(argcp, &(*argv)[1], 2);
  289.         }
  290.         else if(!strcmp(__glutArgv[i], "-gldebug"))
  291.         {
  292.             __glutDebug = GL_TRUE;
  293.             removeArgs(argcp, &(*argv)[1], 1);
  294.         }
  295.         else
  296.         {
  297.             /* Once unknown option encountered, stop command line processing. */
  298.             break;
  299.         }
  300.     }
  301.  
  302.     __glutInitTime(&unused);
  303.     
  304.     __glutInited = GL_TRUE;
  305. }
  306.  
  307. void glutInitWindowPosition(int x, int y)
  308. {
  309.     __glutInitX = x;
  310.     __glutInitY = y;
  311. }
  312.  
  313. void glutInitWindowSize(int width, int height)
  314. {
  315.     __glutInitWidth = width;
  316.     __glutInitHeight = height;
  317. }
  318.  
  319. void glutInitDisplayMode(unsigned int mask)
  320. {
  321.     __glutDisplayMode = mask;
  322. }
  323.